package com.wfh.share;

import java.text.SimpleDateFormat;

import com.weibo.sdk.android.Oauth2AccessToken;
import com.weibo.sdk.android.Weibo;
import com.weibo.sdk.android.WeiboAuthListener;
import com.weibo.sdk.android.WeiboDialogError;
import com.weibo.sdk.android.WeiboException;
import com.wfh.R;
import com.wfh.util.Util;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.webkit.CookieManager;
import android.webkit.CookieSyncManager;
import android.widget.Toast;

public class SinaWeiBo {
	private Weibo mWeibo;
	private static final String CONSUMER_KEY = "966056985";// 1710961109
															// 966056985
															// 替换为开发者的appkey，例如"1646212860";
	private static final String REDIRECT_URL = "http://www.sina.com";
	public static Oauth2AccessToken accessToken;
	public static final String TAG = "sinasdk";
	private Context context;
	private OnCompleteListner listener=null;
	public SinaWeiBo(Context context) {
		mWeibo = Weibo.getInstance(CONSUMER_KEY, REDIRECT_URL);
		this.context = context;
	}
	
	public void setOnCompleteListener(OnCompleteListner listener){
		this.listener=listener;
	}

	public void authorize() {
		mWeibo.authorize(context, new AuthDialogListener());
	}

	class AuthDialogListener implements WeiboAuthListener {

		@Override
		public void onComplete(Bundle values) {
			String token = values.getString("access_token");
			String expires_in = values.getString("expires_in");
			SinaWeiBo.accessToken = new Oauth2AccessToken(token, expires_in);
			if (SinaWeiBo.accessToken.isSessionValid()) {
				String date = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss")
						.format(new java.util.Date(SinaWeiBo.accessToken
								.getExpiresTime()));

				/*Util.showToast(context, "认证成功: \r\n access_token: " + token
						+ "\r\n" + "expires_in: " + expires_in + "\r\n有效期："
						+ date);*/
				try {
					Class sso = Class
							.forName("com.weibo.sdk.android.api.WeiboAPI");// 如果支持weiboapi的话，显示api功能演示入口按钮
					// apiBtn.setVisibility(View.VISIBLE);
				} catch (ClassNotFoundException e) {
					// e.printStackTrace();
					Log.i(TAG, "com.weibo.sdk.android.api.WeiboAPI not found");

				}
				/*
				 * AccessTokenKeeper.keepAccessToken(context, accessToken);
				 */
				Util.saveShare(context, "sinatoken", token);
				Util.saveShare(context, "sinaexpires", expires_in);
				Toast.makeText(context, "认证成功", Toast.LENGTH_SHORT).show();
				if(listener!=null) listener.onComplete();
			}
		}

		@Override
		public void onError(WeiboDialogError e) {
			Toast.makeText(context.getApplicationContext(),
					"Auth error : " + e.getMessage(), Toast.LENGTH_LONG).show();
		}

		@Override
		public void onCancel() {
			Toast.makeText(context.getApplicationContext(), "Auth cancel",
					Toast.LENGTH_LONG).show();
		}

		@Override
		public void onWeiboException(WeiboException e) {
			Toast.makeText(context.getApplicationContext(),
					"Auth exception : " + e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}

	}
	
	//销毁
	public void loginOut(){
		CookieSyncManager cookieSyncMngr = CookieSyncManager.createInstance(context);
		CookieManager cookieManager = CookieManager.getInstance();
		cookieManager.removeAllCookie();
	}

}
